package com.manlost.upload.operate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;

import com.manlost.upload.model.constant.FTPConstant;

/**
 * 类FTPOp.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-5-6 下午1:04:41
 */
public class FTPOp {

    private FTPClient ftpClient;

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void connectDefaultServer() throws IOException {
        if (ftpClient != null) {
            throw new RuntimeException("FTPOp fileClient is not null, don't connect 2 times");
        }
        ftpClient = new FTPClient();
        ftpClient.connect(FTPConstant.SERVER);
        ftpClient.login(FTPConstant.USER, FTPConstant.PASSWORD);
        ftpClient.changeWorkingDirectory(FTPConstant.SERVER_PATH);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.setBufferSize(1024);
        ftpClient.setControlEncoding(FTPConstant.ENCODING);
    }

    /**
     * connectServer
     * 
     * @throws java.io.IOException
     * @param path -- root directory
     * @param password
     * @param user
     * @param server
     */
    public void connectServer(String server, String user, String password, String path) throws IOException {
        if (ftpClient != null) {
            throw new RuntimeException("FTPOp fileClient is not null, don't connect 2 times");
        }
        ftpClient = new FTPClient();
        ftpClient.connect(server);
        ftpClient.login(user, password);
        if (path.length() != 0) ftpClient.changeWorkingDirectory(path);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.setBufferSize(1024);
        ftpClient.setControlEncoding("GBK");
    }

    /**
     * closeServer
     * 
     * @throws java.io.IOException
     */
    public void closeServer() throws IOException {
        try {
            if (ftpClient != null) {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            }
        } catch (IOException e) {
        }
    }

    @Override
    protected void finalize() throws Throwable {
        closeServer();
    }

    /**
     * upload file
     * 
     * @throws IOException
     * @return -1 文件不存在 -2 文件内容为空 >0 成功上传，返回文件的大小
     * @param newname 上传后的新文件名
     * @param filename 上传的文件
     * @throws IOException
     */
    public boolean upload(String localFileName, String remoteFileName) throws IOException {
        if (StringUtils.isBlank(remoteFileName) || StringUtils.isBlank(localFileName)) {
            throw new IllegalArgumentException("upload parameters error");
        }
        return ftpClient.storeFile(remoteFileName, new FileInputStream(new File(localFileName)));
    }

    public boolean cd(String dirName) throws IOException {
        return ftpClient.changeWorkingDirectory(dirName);
    }

    public boolean mkdir(String remoteDirName) throws IOException {
        return ftpClient.makeDirectory(remoteDirName);
    }

    public String pwd() throws IOException {
        return ftpClient.printWorkingDirectory();
    }

    /**
     * upload file
     * 
     * @throws IOException
     * @return -1 文件不存在 -2 文件内容为空 >0 成功上传，返回文件的大小
     * @param newname 上传后的新文件名
     * @param filename 上传的文件
     * @throws IOException
     */
    public boolean upload(InputStream in, String remoteFileName) throws IOException {
        return ftpClient.storeFile(remoteFileName, in);
    }

    /**
     * download
     * 
     * @throws java.lang.IOException
     * @return
     * @param newfilename 本地生成的文件名
     * @param filename 服务器上的文件名
     */
    public boolean download(String remoteFileName, String localFileName) throws IOException {
        if (StringUtils.isBlank(remoteFileName) || StringUtils.isBlank(localFileName)) {
            throw new IllegalArgumentException("download parameters error");
        }
        return ftpClient.retrieveFile(remoteFileName, new FileOutputStream(new File(localFileName)));
    }

}
